<?php

/**
 * Subclass for representing a row from the 'ringtone' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Ringtone extends BaseRingtone
{
	
	protected  $aRingtoneExtra = null;
	
	protected $aRingtoneComment = null;
	
	protected $aRingtoneSynchronize = null;
	
	protected $imuzik_last_downloaded;
	
	protected $imuzik_hidden_time;
	
    public function  __toString() {
        return $this->getName();
    }
    
    public function getRingtoneExtra()
    {
    	$this->aRingtoneExtra = RingtoneExtraPeer::retrieveByPK($this->getId());
    	
    	if (!$this->aRingtoneExtra)
    	{
    		$this->aRingtoneExtra = new RingtoneExtra();
    		$this->aRingtoneExtra->setRingtoneId($this->getId());
		}
    	
    	return $this->aRingtoneExtra;
	}
	
	public function getRingtoneComment()
    {
    	$criteria = new Criteria();
    	$criteria->add(RingtoneCommentPeer::RINGTONE_ID, $this->getId());
    	$results = RingtoneCommentPeer::doSelect($criteria);
    	$this->aRingtoneComment = $results[0];
    	return $this->aRingtoneComment;
	}
	
	public function getOrInitRingtoneStatus()
	{
		$this->getRingtoneStatuss();
		
		if (empty($this->collRingtoneStatuss))
		{
			$rtstatus = new RingtoneStatus();
			$rtstatus->setRingtoneId($this->getId());
			$this->addRingtoneStatus($rtstatus);
			$this->save();
			$this->collRingtoneStatuss[0] = $rtstatus;
		}
		
		$rtstatus = $this->collRingtoneStatuss[0];
		return $rtstatus;
	}
	
	public function getRingtoneOrigin()
	{	
		/* OLD CODE
		$ringtoneOrigin = RingtoneOriginPeer::retrieveByPK($this->getId());
		return $ringtoneOrigin;
		*/
		
		$ringtone_extra = RingtoneExtraPeer::retrieveByPK($this->getId());
		$ringtoneOrigin = RingtoneOriginPeer::retrieveByPK($ringtone_extra->getOriginId());
		return $ringtoneOrigin;
	}
	
    protected function doSave($con)
	{
		
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


												
			if ($this->aUser !== null) {
				if ($this->aUser->isModified()) {
					$affectedRows += $this->aUser->save($con);
				}
				$this->setUser($this->aUser);
				$this->setCreatorName($this->aUser->getUsername());
			}

			if ($this->aRingtoneCategory !== null) {
				if ($this->aRingtoneCategory->isModified()) {
					$affectedRows += $this->aRingtoneCategory->save($con);
				}
				$this->setRingtoneCategory($this->aRingtoneCategory);
			}
			
			if ($this->aRingtoneExtra !== null) {
				if ($this->aRingtoneExtra->isModified()) {
					$affectedRows += $this->aRingtoneExtra->save($con);
				}
			}

			$now = time();
            $now = date( 'Y-m-d H:i:s', $now);
            $this->setLastUpdateDatetime($now);
            
						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = RingtonePeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += RingtonePeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

			if ($this->collRingtoneStatuss !== null) {
				foreach($this->collRingtoneStatuss as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	}

    public function setCategoryId($v)
    {
        parent::setCategoryId($v);
        include_once 'lib/model/RingtoneCategoryPeer.php';
        //$cate = RingtoneCategoryPeer::retrieveByPK($v);
        //$this->setCategoryName($cate->getName());
    }

    public function setCreatorId($v)
    {
        parent::setCreatorId($v);
        include_once 'lib/model/UserPeer.php';
        $cate = UserPeer::retrieveByPK($v);
        $this->setCreatorName($cate->getUsername());
    }
    
    public function getFullAvatarPath() {
        return MEDIA_DOMAIN.'/ringtone/thumbnail/large/' . $this->getAvatarPath();
    }

    public function getFullFilePathMp3() {
        return MEDIA_DOMAIN."/ringtone/realtone/".parent::getFilePathMp3();
    }

    public function getFullFilePathWav() {
        return MEDIA_DOMAIN."/ringtone/rbt/".parent::getFilePathWav();
    }
    
    // Thangtv2 add
    
    public function getRingtoneSynchronize()
    {
    	
    	$this->aRingtoneSynchronize = RingtoneSynchronizePeer::retrieveByPK($this->getId());
    	
    	if (!$this->aRingtoneSynchronize)
    	{
    		$this->aRingtoneSynchronize = new RingtoneSynchronize();
    		$this->aRingtoneSynchronize->setRingId($this->getId());
		}
    	
    	return $this->aRingtoneSynchronize;
	}
	
	public function getTopOrder()
	{
		$con = new MysqlDAL('propel');
        $sql = "SELECT top_order FROM ringtone
            WHERE ringtone.id = '".$this->getId()."'";
        $ring=  $con->doSelect($sql);
        return $ring[0]['top_order'];
	}
	
	public function updatecatename() {
		$cate = RingtoneCategoryPeer::retrieveByPK($this->getCategoryId());
        $con = new MysqlDAL('propel');
        $sql = "UPDATE ringtone set category_name = '{$cate->getName()}' WHERE id = '{$this->getId()}'";
        $con->doupdate($sql);
    }
    
    public function getImuzikLastDownloaded()
    {
    	return $this->imuzik_last_downloaded;
    }
    
    public function getImuzikHiddenTime()
    {
    	return $this->imuzik_hidden_time;
    }
    
}
